package com.bamboo.tool.view.component.actions;

import cn.hutool.core.io.FileUtil;
import com.bamboo.tool.config.BambooToolComponent;
import com.bamboo.tool.config.RestfulCloudApplicationCache;
import com.bamboo.tool.entity.ApiClass;
import com.bamboo.tool.entity.QueryClassParameter;
import com.bamboo.tool.template.export.entity.ExportDirectory;
import com.bamboo.tool.template.export.util.ExportConversionUtil;
import com.bamboo.tool.util.FileGenerationUtil;
import com.bamboo.tool.util.PsiUtils;
import com.bamboo.tool.view.component.tree.ApiTree;
import com.bamboo.tool.view.component.tree.ClassNode;
import com.bamboo.tool.view.component.tree.MethodNode;
import com.intellij.icons.AllIcons;
import com.intellij.ide.util.PsiNavigationSupport;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;

import java.util.*;

/**
 * @author bamboo
 * Date 2022/10/21 18:00
 * Description
 */
public class TemplateDebugAction extends AnAction {
    private ApiTree apiTree;

    public TemplateDebugAction(ApiTree apiTree) {
        super("打开文档", "打开文档", AllIcons.General.OpenDisk);
        this.apiTree = apiTree;
    }

    @Override
    public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
        QueryClassParameter queryClassParam = PsiUtils.getQueryClassParameter(apiTree);
        String defaultPath = RestfulCloudApplicationCache.getExportDefaultPath() + "\\DefaultDocument\\" + UUID.randomUUID() + ".md";
        FileUtil.touch(defaultPath);
        Collection<ApiClass> apiClass = BambooToolComponent.getStoreService().getApiClass(queryClassParam);
        Map<String, List<ExportDirectory>> exportDirectory = ExportConversionUtil.toExportDirectory(apiClass);
        List<ExportDirectory> directors = new ArrayList<>();
        exportDirectory.values().stream().forEach(e -> directors.addAll(e));
        FileGenerationUtil.generation(RestfulCloudApplicationCache.getDataPath() + "\\template\\markdown\\", "md.ftl", defaultPath, directors);

        VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByPath(defaultPath);
        PsiNavigationSupport.getInstance().createNavigatable(anActionEvent.getProject(), file, -1).navigate(true);

    }

    @Override
    public void update(@NotNull AnActionEvent event) {
        Presentation presentation = event.getPresentation();

        if (apiTree.getSelectionCount() == 1) {
            Object lastPathComponent = apiTree.getSelectionPaths()[0].getLastPathComponent();
            if (lastPathComponent instanceof MethodNode || lastPathComponent instanceof ClassNode) {
                presentation.setEnabledAndVisible(true);
            } else {
                presentation.setEnabledAndVisible(false);
            }

        } else {
            presentation.setEnabledAndVisible(false);
        }

    }
}
